package com.fsi.client;

import java.util.List;

import com.fsi.client.util.LoadingScreen;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ApplicationServiceDelegate {
	private final ApplicationServiceAsync service = GWT
			.create(ApplicationService.class);
	ApplicationGUI gui = null;
	public static LoadingScreen loadingScreen = new LoadingScreen();

	public void initLogIn() {
		loadingScreen.center();
		service.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() {

			@Override
			public void onFailure(Throwable caught) {
				loadingScreen.hide();
				gui.service_loginTestFailed(caught);
			}

			@Override
			public void onSuccess(LoginInfo result) {
				loadingScreen.hide();
				gui.service_loginTestSucceeded(result);
			}

		});
	}

	public void listCustomers(final ServiceGUI source) {
		service.listCustomers(new AsyncCallback<List<Customer>>() {
			public void onFailure(Throwable caught) {
				source.service_ListContactsFailed(caught);
			}

			public void onSuccess(List<Customer> result) {
				source.service_ListRetrievedFromService(result);

			}
		}// end of inner class
				);// end of listContacts method call.
	}

	public void addCustomer(final ServiceGUI source, final Customer customer) {
		service.addNewCustomer(customer, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				source.service_addFailed(caught.toString());
			}

			@Override
			public void onSuccess(Void result) {
				source.service_addSucceeded(customer);

			}

		});
	}

	public void deleteCustomer(final ServiceGUI source, Customer customer) {
		service.deleteCustomer(customer, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				source.service_deleteFailed(caught);
			}

			@Override
			public void onSuccess(Void result) {
				source.service_deleteSucceeded();
			}

		});
	}

	public void updateFixedCustomer(final ServiceGUI source, Customer customer) {
		service.updateCustomerDetails(customer, Customer.UPDATEFIXED,
				new AsyncCallback<Customer>() {

					@Override
					public void onFailure(Throwable caught) {
						source.service_updateFixedFailed(caught.getMessage());
					}

					@Override
					public void onSuccess(Customer result) {
						source.service_afterUpdateFixed();
					}

				});

	}

	public void updateGeneralDetails(final ServiceGUI source, Customer customer) {
		service.updateCustomerDetails(customer, Customer.UPDATEGENERAL,
				new AsyncCallback<Customer>() {

					@Override
					public void onFailure(Throwable caught) {
						source.service_updateGeneralFailed(caught.getMessage());
					}

					@Override
					public void onSuccess(Customer result) {
						source.service_afterUpdateGeneral();
					}

				});
	}

	public void getPhotos(final ServiceGUI source, String customerID) {
		service.getPhotos(customerID, new AsyncCallback<List<Shuttle>>() {

			@Override
			public void onFailure(Throwable caught) {
				System.err.println("Error getting photos: "
						+ caught.getMessage());
			}

			@Override
			public void onSuccess(List<Shuttle> result) {
				source.service_getPhotoIDs(result);

			}

		});

	}

	public void getCustomer(String customerID, AsyncCallback<Customer> callback) {
		service.getCustomerByEmail(customerID, callback);
	}

	public void getCustomer(String customerID, final int returnMethod) {
		loadingScreen.center();

		service.getCustomerByEmail(customerID, new AsyncCallback<Customer>() {

			@Override
			public void onSuccess(Customer customer) {
				loadingScreen.hide();
				switch (returnMethod) {
				case ApplicationGUI.SHOWPERSONAL_METHOD:
					gui.service_showPersonal(customer);
					break;
				case ApplicationGUI.SHOWEDITCUSTOMER_METHOD:
					gui.service_showEditCustomer(customer);
					break;
				case ApplicationGUI.LOGINSUCCEED:
					gui.setStatus("get customer, login succeeded");
					gui.afterLoginAndCustomer(customer);
					break;
				case ApplicationGUI.SHOWPHOTOS_METHOD:
					gui.service_showPhotos(customer);
					break;
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				loadingScreen.hide();
				switch (returnMethod) {
				case (ApplicationGUI.LOGINSUCCEED):
					gui.service_afterGetByEmailFailed();
					break;
				default:
					gui.setStatus("Error locating customer: "
							+ caught.getMessage());
					break;
				}
			}

		});

	}

	public void deletePhoto(final String customerID, String photoID) {
		service.deletePhoto(customerID, photoID, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				gui
						.setStatus("Error on deleting image: "
								+ caught.getMessage());
				gui.showPhotos(true);
			}

			@Override
			public void onSuccess(Void result) {
				gui.showPhotos(true);
			}
		});
	}
}
